package com.bdqn.petmalldemo.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.petmalldemo.common.ReturnResult;
import com.bdqn.petmalldemo.common.SystemConstants;
import com.bdqn.petmalldemo.common.TokenCache;
import com.bdqn.petmalldemo.entity.OrderDetail;
import com.bdqn.petmalldemo.entity.User;
import com.bdqn.petmalldemo.service.OrderDetailService;
import com.bdqn.petmalldemo.utils.CommonUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lenovo
 * @since 2020-02-10
 */
@RestController
@RequestMapping("/order/detail")
public class OrderDetailController {
    @Resource
    private OrderDetailService orderDetailService;

    @ApiOperation(value = "订单详情")
    @PostMapping(value = "/list")
    public ReturnResult getOrderDetail(String token, String orderId){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("order_id",orderId);
        List<OrderDetail> orderDetailList = orderDetailService.list(orderDetailQueryWrapper);
        return returnResult.data(orderDetailList);
    }


    @ApiOperation(value = "卖家获取订单列表")
    @PostMapping(value = "/search")
    public ReturnResult getOrderBySellerId(String token,Integer currentPage,Integer role){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        User user = JSONObject.parseObject(TokenCache.get(token), User.class);
        QueryWrapper<OrderDetail> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.eq("seller_id",user.getId());
        //封装分页对象
        IPage<OrderDetail>page=new Page<>(StringUtils.isEmpty(currentPage)?0:currentPage, SystemConstants.PAGE_SIZE);
        orderDetailService.page(page,orderDetailQueryWrapper);
        Map<String,Object> map=new HashMap<>(16);
        map.put("list",page.getRecords());
        map.put("total",page.getTotal());
        map.put("pages",page.getPages());
        map.put("pageNum",page.getCurrent());
        return  returnResult.data(map);
    }

    @ApiOperation(value = "部分发货")
    @PostMapping("send")
    public ReturnResult send(String token,String orderDetailId){
        ReturnResult returnResult = CommonUtils.checkUserLogin(token);
        orderDetailService.send(orderDetailId);
        return returnResult;
    }
}

